//
//  ViewController.m
//  demo
//
//  Created by vonchenchen on 2021/3/18.
//  Copyright © 2021 vonchenchen. All rights reserved.
//

#import "ViewController.h"
#include "CubeDrawer.h"
#include "ViewCalculator.hpp"

@interface ViewController(){
    
    std::unique_ptr<CubeDrawer> _drawer;
    
    Vec<3, float> cam_up;
    Vec<3, float> cam_eye;
    Vec<3, float> cam_at;
    
    ViewCalculator _view_calcu;
    
    NSBitmapImageRep *bitmap;
    int width;
    int height;
    
    const char *path;
}
@property (weak) IBOutlet NSImageView *main_view;
@property (weak) IBOutlet NSButton *btn_rotate_left;
@property (weak) IBOutlet NSButton *btn_rotate_top;
@property (weak) IBOutlet NSButton *btn_rotate_z;
@property (weak) IBOutlet NSButton *btn_light_enble;
@property (weak) IBOutlet NSButton *btn_antialising;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    width=300;
    height=300;
    
    _drawer = std::make_unique<CubeDrawer>();
    _drawer->Init(width, height);
    
    cam_up = {0, 1, 0};
    cam_eye = {5, 5, 5};
    cam_at = {0, 0, 0};
    
    _view_calcu.InitEyePos(cam_eye);
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    documentPath = [documentPath stringByAppendingString:@"/test.bmp"];
    
    path = [documentPath UTF8String];
    
    uint8_t *ptr = _drawer->GetFrameBuffer()->GetBits();

    bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&ptr pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:width*4 bitsPerPixel:32];

    [self requestDraw];
}

- (void)requestDraw{
    
    _drawer->SetCameraPos(cam_up, cam_eye, cam_at);
    _drawer->SetPersPro(45, 1, 0.1, 50);
    
    _drawer->RequestRender(path);
    
    uint8_t *ptr = _drawer->GetFrameBuffer()->GetBits();
    bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&ptr pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:width*4 bitsPerPixel:32];
    
    NSImage *img = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];
    [img addRepresentation:bitmap];
    _main_view.image = img;
}

- (IBAction)onClieckRotateLeft:(id)sender {
    NSLog(@"RotateLeft");
    //绕y轴向右
    cam_eye = _view_calcu.RotateY(cam_eye, 10);
    [self requestDraw];
}

- (IBAction)onClickRotateTop:(id)sender {
    NSLog(@"RotateTop");
    //绕x轴向上
    cam_eye = _view_calcu.RotateX(cam_eye, 10);
    [self requestDraw];
}

- (IBAction)onClickRotateZ:(id)sender {
    NSLog(@"RotateTop");
    cam_eye = _view_calcu.RotateZ(cam_eye, 10);
    [self requestDraw];
}

- (IBAction)onClickLightEnable:(id)sender {
    
    bool enable = !_drawer->GetLightEnable();
    _drawer->SetLightEnable(enable);
    [self requestDraw];
}

- (IBAction)onClickAntiAliasing:(id)sender {
    
    bool enable = !_drawer->GetAntiAliasing();
    _drawer->SetAntiAliasing(enable);
    [self requestDraw];
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}



@end
